package com.qiwenshare.file.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @program: qiwen-file
 * @description: SpringBeanUtil
 * @author: MJ
 * @create: 2023-09-16 22:39
 **/
public class SpringBeanUtil {
    private static final Logger log = LoggerFactory.getLogger(SpringBeanUtil.class);

    private SpringBeanUtil() {
    }

    public static <T, D> T copyProperties(D sourceObj, Class<T> clazz) {
        if (sourceObj == null) {
            return null;
        } else {
            T t = null;

            try {
                t = clazz.newInstance();
                BeanUtils.copyProperties(sourceObj, t);
            } catch (IllegalAccessException var4) {
                log.warn("自动转换失败", var4);
            } catch (InstantiationException var5) {
                log.warn("自动转换失败", var5);
            }

            return t;
        }
    }

    public static <T, D> List<T> copy(List<D> sourceObjs, Class<T> clazz) {
        if (sourceObjs == null) {
            return Collections.emptyList();
        } else {
            int len = sourceObjs.size();
            List list = new ArrayList(len);
            Object t = null;

            for(int i = 0; i < len; ++i) {
                Object d = sourceObjs.get(i);
                t = copyProperties(d, clazz);
                list.add(t);
            }

            return list;
        }
    }

    public static Map<String, String> convertToMap(Object obj) {
        try {
            if (obj instanceof Map) {
                return (Map)obj;
            }

            Map<String, String> returnMap = org.apache.commons.beanutils.BeanUtils.describe(obj);
            returnMap.remove("class");
            return returnMap;
        } catch (IllegalAccessException var2) {
            var2.getMessage();
        } catch (InvocationTargetException var3) {
            var3.getMessage();
        } catch (NoSuchMethodException var4) {
            var4.getMessage();
        }

        return new HashMap();
    }

    public static boolean hasNonNullProperties(Object obj) {
        Map map = convertToMap(obj);
        Iterator var2 = map.values().iterator();

        Object o;
        do {
            if (!var2.hasNext()) {
                return false;
            }

            o = var2.next();
        } while(o == null);

        return true;
    }

//    public static Map convertValue2List(Map map) {
//        Map rtnMap = new HashMap();
//        Iterator var3 = map.keySet().iterator();
//
//        while(true) {
//            while(var3.hasNext()) {
//                Object key = var3.next();
//                if (key.toString().contains("List") && StringUtils.isNotBlank((CharSequence)map.get(key))) {
//                    JSONArray jsonArray = JSONObject.parseArray(map.get(key).toString());
//                    rtnMap.put(key, jsonArray);
//                } else {
//                    rtnMap.put(key, map.get(key));
//                }
//            }
//
//            return rtnMap;
//        }
//    }
}
